package com.buaa.aimall.userModule.Service;

import com.buaa.aimall.Utils.Config.JeepayConfig;
import com.buaa.aimall.userModule.Entity.UserEntity;
import com.jeequan.jeepay.JeepayClient;
import com.jeequan.jeepay.exception.JeepayException;
import com.jeequan.jeepay.model.PayOrderCreateReqModel;
import com.jeequan.jeepay.model.PayOrderQueryReqModel;
import com.jeequan.jeepay.model.TransferOrderCreateReqModel;
import com.jeequan.jeepay.request.PayOrderCreateRequest;
import com.jeequan.jeepay.request.PayOrderQueryRequest;
import com.jeequan.jeepay.request.TransferOrderCreateRequest;
import com.jeequan.jeepay.response.PayOrderCreateResponse;
import com.jeequan.jeepay.response.PayOrderQueryResponse;
import com.jeequan.jeepay.response.TransferOrderCreateResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

@Service
public class JeepayService {
    private final JeepayConfig jeepayConfig;

    @Autowired
    public JeepayService(JeepayConfig jeepayConfig) {
        this.jeepayConfig = jeepayConfig;
    }

    public PayOrderCreateResponse recharge(int amount, UserEntity user) {
        JeepayClient jeepayClient = JeepayClient.getInstance(
                jeepayConfig.getAppId(),
                jeepayConfig.getApiKey(),
                jeepayConfig.getApiBase());

        // 构建请求数据
        String wayCode = "ALI_PC";                           // 支付方式
        PayOrderCreateRequest request = new PayOrderCreateRequest();
        PayOrderCreateReqModel model = new PayOrderCreateReqModel();
        model.setMchNo(jeepayConfig.getMchNo());                       // 商户号
        model.setAppId(jeepayClient.getAppId());            // 应用ID
        String orderNo = "mho" + new Date().getTime();
        model.setMchOrderNo(orderNo);                       // 商户订单号
        model.setWayCode(wayCode);                          // 支付方式
        model.setAmount((long) amount);                                // 金额，单位分
        model.setCurrency("CNY");                           // 币种，目前只支持cny
        model.setClientIp("127.0.0.1");                 // 发起支付请求客户端的IP地址
        model.setSubject("AI资源超市充值");                         // 商品标题
        model.setBody("为用户 " + user.getUsername() + " 充值");                            // 商品描述
        model.setNotifyUrl("http://www.cheesechise.top:13001/user/jeepay/recharge-notify");      // 异步通知地址
        model.setReturnUrl("http://www.cheesechise.top");                             // 前端跳转地址
        model.setExtParam("");                              // 商户扩展参数,回调时原样返回
        model.setExpiredTime("36000");
        request.setBizModel(model);

        // 发起统一下单
        PayOrderCreateResponse response = null;
        try {
            response = jeepayClient.execute(request);
        } catch (JeepayException e) {
            e.printStackTrace();
        }
        return response;
    }

    public PayOrderQueryResponse query(String mchOrderNo) {
        JeepayClient jeepayClient = JeepayClient.getInstance(
                jeepayConfig.getAppId(),
                jeepayConfig.getApiKey(),
                jeepayConfig.getApiBase());

        PayOrderQueryRequest request = new PayOrderQueryRequest();
        PayOrderQueryReqModel model = new PayOrderQueryReqModel();

        model.setMchNo(jeepayConfig.getMchNo());                       // 商户号
        model.setAppId(jeepayConfig.getAppId());            // 应用ID
        model.setMchOrderNo(mchOrderNo);                       // 商户订单号
        request.setBizModel(model);

        PayOrderQueryResponse response = null;
        try {
            response = jeepayClient.execute(request);
        } catch (JeepayException e) {
            e.printStackTrace();
        }
        return response;
    }

    public TransferOrderCreateResponse withdrawalTransfer(int amount, UserEntity user, String accountNo, String accountName) {
        JeepayClient jeepayClient = JeepayClient.getInstance(
                jeepayConfig.getAppId(),
                jeepayConfig.getApiKey(),
                jeepayConfig.getApiBase());

        TransferOrderCreateRequest request = new TransferOrderCreateRequest();
        TransferOrderCreateReqModel model = new TransferOrderCreateReqModel();

        model.setMchNo(jeepayConfig.getMchNo());
        model.setAppId(jeepayConfig.getAppId());
        String mchOrderNo = "mho" + new Date().getTime();
        model.setMchOrderNo(mchOrderNo);                       // 商户订单号
        model.setIfCode("alipay");
        model.setEntryType("ALIPAY_CASH");
        model.setAmount((long) amount);
        model.setCurrency("CNY");
        model.setAccountNo(accountNo);
        model.setAccountName(accountName);
        model.setClientIp("127.0.0.1");
        model.setTransferDesc("AIMall收益提现转账");
        model.setNotifyUrl("http://www.cheesechise.top:13001/user/jeepay/transfer-notify");
        request.setBizModel(model);

        TransferOrderCreateResponse response = null;
        try {
            response = jeepayClient.execute(request);
        } catch (JeepayException e) {
            e.printStackTrace();
        }
        return response;
    }
}
